<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - Lua 5.1.5 - src/ldblib.c</title>
  <link rel="stylesheet" type="text/css" href="../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../index.html">top level</a> - <a href="index.html">src</a> - ldblib.c</td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">Lua 5.1.5</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">216</td>
            <td class="headerCovTableEntry">229</td>
            <td class="headerCovTableEntryHi">94.3 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2019-06-13 19:44:15</td>
            <td></td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<span class="lineNum">       1 </span>            : /*
<span class="lineNum">       2 </span>            : ** $Id: ldblib.c,v 1.104.1.4 2009/08/04 18:50:18 roberto Exp $
<span class="lineNum">       3 </span>            : ** Interface from Lua to its debug API
<span class="lineNum">       4 </span>            : ** See Copyright Notice in lua.h
<span class="lineNum">       5 </span>            : */
<span class="lineNum">       6 </span>            : 
<span class="lineNum">       7 </span>            : 
<span class="lineNum">       8 </span>            : #include &lt;stdio.h&gt;
<span class="lineNum">       9 </span>            : #include &lt;stdlib.h&gt;
<span class="lineNum">      10 </span>            : #include &lt;string.h&gt;
<span class="lineNum">      11 </span>            : 
<span class="lineNum">      12 </span>            : #define ldblib_c
<span class="lineNum">      13 </span>            : #define LUA_LIB
<span class="lineNum">      14 </span>            : 
<span class="lineNum">      15 </span>            : #include &quot;lua.h&quot;
<span class="lineNum">      16 </span>            : 
<span class="lineNum">      17 </span>            : #include &quot;lauxlib.h&quot;
<span class="lineNum">      18 </span>            : #include &quot;lualib.h&quot;
<span class="lineNum">      19 </span>            : 
<span class="lineNum">      20 </span>            : 
<span class="lineNum">      21 </span>            : 
<span class="lineNum">      22 </span><span class="lineCov">          1 : static int db_getregistry (lua_State *L) {</span>
<span class="lineNum">      23 </span><span class="lineCov">          1 :   lua_pushvalue(L, LUA_REGISTRYINDEX);</span>
<span class="lineNum">      24 </span><span class="lineCov">          1 :   return 1;</span>
<span class="lineNum">      25 </span>            : }
<span class="lineNum">      26 </span>            : 
<span class="lineNum">      27 </span>            : 
<span class="lineNum">      28 </span><span class="lineCov">          6 : static int db_getmetatable (lua_State *L) {</span>
<span class="lineNum">      29 </span><span class="lineCov">          6 :   luaL_checkany(L, 1);</span>
<span class="lineNum">      30 </span><span class="lineCov">          6 :   if (!lua_getmetatable(L, 1)) {</span>
<span class="lineNum">      31 </span><span class="lineCov">          3 :     lua_pushnil(L);  /* no metatable */</span>
<span class="lineNum">      32 </span>            :   }
<span class="lineNum">      33 </span><span class="lineCov">          6 :   return 1;</span>
<span class="lineNum">      34 </span>            : }
<span class="lineNum">      35 </span>            : 
<span class="lineNum">      36 </span>            : 
<span class="lineNum">      37 </span><span class="lineCov">          5 : static int db_setmetatable (lua_State *L) {</span>
<span class="lineNum">      38 </span><span class="lineCov">          5 :   int t = lua_type(L, 2);</span>
<span class="lineNum">      39 </span><span class="lineCov">          5 :   luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,</span>
<span class="lineNum">      40 </span>            :                     &quot;nil or table expected&quot;);
<span class="lineNum">      41 </span><span class="lineCov">          4 :   lua_settop(L, 2);</span>
<span class="lineNum">      42 </span><span class="lineCov">          4 :   lua_pushboolean(L, lua_setmetatable(L, 1));</span>
<span class="lineNum">      43 </span><span class="lineCov">          4 :   return 1;</span>
<span class="lineNum">      44 </span>            : }
<span class="lineNum">      45 </span>            : 
<span class="lineNum">      46 </span>            : 
<span class="lineNum">      47 </span><span class="lineCov">         13 : static int db_getfenv (lua_State *L) {</span>
<span class="lineNum">      48 </span><span class="lineCov">         13 :   luaL_checkany(L, 1);</span>
<span class="lineNum">      49 </span><span class="lineCov">         13 :   lua_getfenv(L, 1);</span>
<span class="lineNum">      50 </span><span class="lineCov">         13 :   return 1;</span>
<span class="lineNum">      51 </span>            : }
<span class="lineNum">      52 </span>            : 
<span class="lineNum">      53 </span>            : 
<span class="lineNum">      54 </span><span class="lineCov">          4 : static int db_setfenv (lua_State *L) {</span>
<span class="lineNum">      55 </span><span class="lineCov">          4 :   luaL_checktype(L, 2, LUA_TTABLE);</span>
<span class="lineNum">      56 </span><span class="lineCov">          4 :   lua_settop(L, 2);</span>
<span class="lineNum">      57 </span><span class="lineCov">          4 :   if (lua_setfenv(L, 1) == 0)</span>
<span class="lineNum">      58 </span><span class="lineCov">          1 :     luaL_error(L, LUA_QL(&quot;setfenv&quot;)</span>
<span class="lineNum">      59 </span>            :                   &quot; cannot change environment of given object&quot;);
<span class="lineNum">      60 </span><span class="lineCov">          3 :   return 1;</span>
<span class="lineNum">      61 </span>            : }
<span class="lineNum">      62 </span>            : 
<span class="lineNum">      63 </span>            : 
<span class="lineNum">      64 </span><span class="lineCov">         10 : static void settabss (lua_State *L, const char *i, const char *v) {</span>
<span class="lineNum">      65 </span><span class="lineCov">         10 :   lua_pushstring(L, v);</span>
<span class="lineNum">      66 </span><span class="lineCov">         10 :   lua_setfield(L, -2, i);</span>
<span class="lineNum">      67 </span><span class="lineCov">         10 : }</span>
<span class="lineNum">      68 </span>            : 
<span class="lineNum">      69 </span>            : 
<span class="lineNum">      70 </span><span class="lineCov">          8 : static void settabsi (lua_State *L, const char *i, int v) {</span>
<span class="lineNum">      71 </span><span class="lineCov">          8 :   lua_pushinteger(L, v);</span>
<span class="lineNum">      72 </span><span class="lineCov">          8 :   lua_setfield(L, -2, i);</span>
<span class="lineNum">      73 </span><span class="lineCov">          8 : }</span>
<span class="lineNum">      74 </span>            : 
<span class="lineNum">      75 </span>            : 
<span class="lineNum">      76 </span><span class="lineCov">         23 : static lua_State *getthread (lua_State *L, int *arg) {</span>
<span class="lineNum">      77 </span><span class="lineCov">         23 :   if (lua_isthread(L, 1)) {</span>
<span class="lineNum">      78 </span><span class="lineCov">          1 :     *arg = 1;</span>
<span class="lineNum">      79 </span><span class="lineCov">          1 :     return lua_tothread(L, 1);</span>
<span class="lineNum">      80 </span>            :   }
<span class="lineNum">      81 </span>            :   else {
<span class="lineNum">      82 </span><span class="lineCov">         22 :     *arg = 0;</span>
<span class="lineNum">      83 </span><span class="lineCov">         22 :     return L;</span>
<span class="lineNum">      84 </span>            :   }
<span class="lineNum">      85 </span>            : }
<span class="lineNum">      86 </span>            : 
<span class="lineNum">      87 </span>            : 
<span class="lineNum">      88 </span><span class="lineCov">          3 : static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {</span>
<span class="lineNum">      89 </span><span class="lineCov">          3 :   if (L == L1) {</span>
<span class="lineNum">      90 </span><span class="lineCov">          3 :     lua_pushvalue(L, -2);</span>
<span class="lineNum">      91 </span><span class="lineCov">          3 :     lua_remove(L, -3);</span>
<span class="lineNum">      92 </span>            :   }
<span class="lineNum">      93 </span>            :   else
<span class="lineNum">      94 </span><span class="lineNoCov">          0 :     lua_xmove(L1, L, 1);</span>
<span class="lineNum">      95 </span><span class="lineCov">          3 :   lua_setfield(L, -2, fname);</span>
<span class="lineNum">      96 </span><span class="lineCov">          3 : }</span>
<span class="lineNum">      97 </span>            : 
<span class="lineNum">      98 </span>            : 
<span class="lineNum">      99 </span><span class="lineCov">          6 : static int db_getinfo (lua_State *L) {</span>
<span class="lineNum">     100 </span>            :   lua_Debug ar;
<span class="lineNum">     101 </span>            :   int arg;
<span class="lineNum">     102 </span><span class="lineCov">          6 :   lua_State *L1 = getthread(L, &amp;arg);</span>
<span class="lineNum">     103 </span><span class="lineCov">          6 :   const char *options = luaL_optstring(L, arg+2, &quot;flnSu&quot;);</span>
<span class="lineNum">     104 </span><span class="lineCov">          6 :   if (lua_isnumber(L, arg+1)) {</span>
<span class="lineNum">     105 </span><span class="lineCov">          2 :     if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &amp;ar)) {</span>
<span class="lineNum">     106 </span><span class="lineCov">          1 :       lua_pushnil(L);  /* level out of range */</span>
<span class="lineNum">     107 </span><span class="lineCov">          1 :       return 1;</span>
<span class="lineNum">     108 </span>            :     }
<span class="lineNum">     109 </span>            :   }
<span class="lineNum">     110 </span><span class="lineCov">          4 :   else if (lua_isfunction(L, arg+1)) {</span>
<span class="lineNum">     111 </span><span class="lineCov">          3 :     lua_pushfstring(L, &quot;&gt;%s&quot;, options);</span>
<span class="lineNum">     112 </span><span class="lineCov">          3 :     options = lua_tostring(L, -1);</span>
<span class="lineNum">     113 </span><span class="lineCov">          3 :     lua_pushvalue(L, arg+1);</span>
<span class="lineNum">     114 </span><span class="lineCov">          3 :     lua_xmove(L, L1, 1);</span>
<span class="lineNum">     115 </span>            :   }
<span class="lineNum">     116 </span>            :   else
<span class="lineNum">     117 </span><span class="lineCov">          1 :     return luaL_argerror(L, arg+1, &quot;function or level expected&quot;);</span>
<span class="lineNum">     118 </span><span class="lineCov">          4 :   if (!lua_getinfo(L1, options, &amp;ar))</span>
<span class="lineNum">     119 </span><span class="lineCov">          1 :     return luaL_argerror(L, arg+2, &quot;invalid option&quot;);</span>
<span class="lineNum">     120 </span><span class="lineCov">          3 :   lua_createtable(L, 0, 2);</span>
<span class="lineNum">     121 </span><span class="lineCov">          3 :   if (strchr(options, 'S')) {</span>
<span class="lineNum">     122 </span><span class="lineCov">          2 :     settabss(L, &quot;source&quot;, ar.source);</span>
<span class="lineNum">     123 </span><span class="lineCov">          2 :     settabss(L, &quot;short_src&quot;, ar.short_src);</span>
<span class="lineNum">     124 </span><span class="lineCov">          2 :     settabsi(L, &quot;linedefined&quot;, ar.linedefined);</span>
<span class="lineNum">     125 </span><span class="lineCov">          2 :     settabsi(L, &quot;lastlinedefined&quot;, ar.lastlinedefined);</span>
<span class="lineNum">     126 </span><span class="lineCov">          2 :     settabss(L, &quot;what&quot;, ar.what);</span>
<span class="lineNum">     127 </span>            :   }
<span class="lineNum">     128 </span><span class="lineCov">          3 :   if (strchr(options, 'l'))</span>
<span class="lineNum">     129 </span><span class="lineCov">          2 :     settabsi(L, &quot;currentline&quot;, ar.currentline);</span>
<span class="lineNum">     130 </span><span class="lineCov">          3 :   if (strchr(options, 'u'))</span>
<span class="lineNum">     131 </span><span class="lineCov">          2 :     settabsi(L, &quot;nups&quot;, ar.nups);</span>
<span class="lineNum">     132 </span><span class="lineCov">          3 :   if (strchr(options, 'n')) {</span>
<span class="lineNum">     133 </span><span class="lineCov">          2 :     settabss(L, &quot;name&quot;, ar.name);</span>
<span class="lineNum">     134 </span><span class="lineCov">          2 :     settabss(L, &quot;namewhat&quot;, ar.namewhat);</span>
<span class="lineNum">     135 </span>            :   }
<span class="lineNum">     136 </span><span class="lineCov">          3 :   if (strchr(options, 'L'))</span>
<span class="lineNum">     137 </span><span class="lineCov">          1 :     treatstackoption(L, L1, &quot;activelines&quot;);</span>
<span class="lineNum">     138 </span><span class="lineCov">          3 :   if (strchr(options, 'f'))</span>
<span class="lineNum">     139 </span><span class="lineCov">          2 :     treatstackoption(L, L1, &quot;func&quot;);</span>
<span class="lineNum">     140 </span><span class="lineCov">          3 :   return 1;  /* return table */</span>
<span class="lineNum">     141 </span>            : }
<span class="lineNum">     142 </span>            :     
<span class="lineNum">     143 </span>            : 
<span class="lineNum">     144 </span><span class="lineCov">          2 : static int db_getlocal (lua_State *L) {</span>
<span class="lineNum">     145 </span>            :   int arg;
<span class="lineNum">     146 </span><span class="lineCov">          2 :   lua_State *L1 = getthread(L, &amp;arg);</span>
<span class="lineNum">     147 </span>            :   lua_Debug ar;
<span class="lineNum">     148 </span>            :   const char *name;
<span class="lineNum">     149 </span><span class="lineCov">          2 :   if (!lua_getstack(L1, luaL_checkint(L, arg+1), &amp;ar))  /* out of range? */</span>
<span class="lineNum">     150 </span><span class="lineCov">          1 :     return luaL_argerror(L, arg+1, &quot;level out of range&quot;);</span>
<span class="lineNum">     151 </span><span class="lineCov">          1 :   name = lua_getlocal(L1, &amp;ar, luaL_checkint(L, arg+2));</span>
<span class="lineNum">     152 </span><span class="lineCov">          1 :   if (name) {</span>
<span class="lineNum">     153 </span><span class="lineCov">          1 :     lua_xmove(L1, L, 1);</span>
<span class="lineNum">     154 </span><span class="lineCov">          1 :     lua_pushstring(L, name);</span>
<span class="lineNum">     155 </span><span class="lineCov">          1 :     lua_pushvalue(L, -2);</span>
<span class="lineNum">     156 </span><span class="lineCov">          1 :     return 2;</span>
<span class="lineNum">     157 </span>            :   }
<span class="lineNum">     158 </span>            :   else {
<span class="lineNum">     159 </span><span class="lineNoCov">          0 :     lua_pushnil(L);</span>
<span class="lineNum">     160 </span><span class="lineNoCov">          0 :     return 1;</span>
<span class="lineNum">     161 </span>            :   }
<span class="lineNum">     162 </span>            : }
<span class="lineNum">     163 </span>            : 
<span class="lineNum">     164 </span>            : 
<span class="lineNum">     165 </span><span class="lineCov">          3 : static int db_setlocal (lua_State *L) {</span>
<span class="lineNum">     166 </span>            :   int arg;
<span class="lineNum">     167 </span><span class="lineCov">          3 :   lua_State *L1 = getthread(L, &amp;arg);</span>
<span class="lineNum">     168 </span>            :   lua_Debug ar;
<span class="lineNum">     169 </span><span class="lineCov">          3 :   if (!lua_getstack(L1, luaL_checkint(L, arg+1), &amp;ar))  /* out of range? */</span>
<span class="lineNum">     170 </span><span class="lineCov">          1 :     return luaL_argerror(L, arg+1, &quot;level out of range&quot;);</span>
<span class="lineNum">     171 </span><span class="lineCov">          2 :   luaL_checkany(L, arg+3);</span>
<span class="lineNum">     172 </span><span class="lineCov">          2 :   lua_settop(L, arg+3);</span>
<span class="lineNum">     173 </span><span class="lineCov">          2 :   lua_xmove(L, L1, 1);</span>
<span class="lineNum">     174 </span><span class="lineCov">          2 :   lua_pushstring(L, lua_setlocal(L1, &amp;ar, luaL_checkint(L, arg+2)));</span>
<span class="lineNum">     175 </span><span class="lineCov">          2 :   return 1;</span>
<span class="lineNum">     176 </span>            : }
<span class="lineNum">     177 </span>            : 
<span class="lineNum">     178 </span>            : 
<span class="lineNum">     179 </span><span class="lineCov">          3 : static int auxupvalue (lua_State *L, int get) {</span>
<span class="lineNum">     180 </span>            :   const char *name;
<span class="lineNum">     181 </span><span class="lineCov">          3 :   int n = luaL_checkint(L, 2);</span>
<span class="lineNum">     182 </span><span class="lineCov">          3 :   luaL_checktype(L, 1, LUA_TFUNCTION);</span>
<span class="lineNum">     183 </span><span class="lineCov">          3 :   if (lua_iscfunction(L, 1)) return 0;  /* cannot touch C upvalues from Lua */</span>
<span class="lineNum">     184 </span><span class="lineCov">          3 :   name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);</span>
<span class="lineNum">     185 </span><span class="lineCov">          3 :   if (name == NULL) return 0;</span>
<span class="lineNum">     186 </span><span class="lineCov">          2 :   lua_pushstring(L, name);</span>
<span class="lineNum">     187 </span><span class="lineCov">          2 :   lua_insert(L, -(get+1));</span>
<span class="lineNum">     188 </span><span class="lineCov">          2 :   return get + 1;</span>
<span class="lineNum">     189 </span>            : }
<span class="lineNum">     190 </span>            : 
<span class="lineNum">     191 </span>            : 
<span class="lineNum">     192 </span><span class="lineCov">          1 : static int db_getupvalue (lua_State *L) {</span>
<span class="lineNum">     193 </span><span class="lineCov">          1 :   return auxupvalue(L, 1);</span>
<span class="lineNum">     194 </span>            : }
<span class="lineNum">     195 </span>            : 
<span class="lineNum">     196 </span>            : 
<span class="lineNum">     197 </span><span class="lineCov">          2 : static int db_setupvalue (lua_State *L) {</span>
<span class="lineNum">     198 </span><span class="lineCov">          2 :   luaL_checkany(L, 3);</span>
<span class="lineNum">     199 </span><span class="lineCov">          2 :   return auxupvalue(L, 0);</span>
<span class="lineNum">     200 </span>            : }
<span class="lineNum">     201 </span>            : 
<span class="lineNum">     202 </span>            : 
<span class="lineNum">     203 </span>            : 
<span class="lineNum">     204 </span>            : static const char KEY_HOOK = 'h';
<span class="lineNum">     205 </span>            : 
<span class="lineNum">     206 </span>            : 
<span class="lineNum">     207 </span><span class="lineCov">        233 : static void hookf (lua_State *L, lua_Debug *ar) {</span>
<span class="lineNum">     208 </span>            :   static const char *const hooknames[] =
<span class="lineNum">     209 </span>            :     {&quot;call&quot;, &quot;return&quot;, &quot;line&quot;, &quot;count&quot;, &quot;tail return&quot;};
<span class="lineNum">     210 </span><span class="lineCov">        233 :   lua_pushlightuserdata(L, (void *)&amp;KEY_HOOK);</span>
<span class="lineNum">     211 </span><span class="lineCov">        233 :   lua_rawget(L, LUA_REGISTRYINDEX);</span>
<span class="lineNum">     212 </span><span class="lineCov">        233 :   lua_pushlightuserdata(L, L);</span>
<span class="lineNum">     213 </span><span class="lineCov">        233 :   lua_rawget(L, -2);</span>
<span class="lineNum">     214 </span><span class="lineCov">        233 :   if (lua_isfunction(L, -1)) {</span>
<span class="lineNum">     215 </span><span class="lineCov">        233 :     lua_pushstring(L, hooknames[(int)ar-&gt;event]);</span>
<span class="lineNum">     216 </span><span class="lineCov">        233 :     if (ar-&gt;currentline &gt;= 0)</span>
<span class="lineNum">     217 </span><span class="lineNoCov">          0 :       lua_pushinteger(L, ar-&gt;currentline);</span>
<span class="lineNum">     218 </span><span class="lineCov">        233 :     else lua_pushnil(L);</span>
<span class="lineNum">     219 </span>            :     lua_assert(lua_getinfo(L, &quot;lS&quot;, ar));
<span class="lineNum">     220 </span><span class="lineCov">        233 :     lua_call(L, 2, 0);</span>
<span class="lineNum">     221 </span>            :   }
<span class="lineNum">     222 </span><span class="lineCov">        233 : }</span>
<span class="lineNum">     223 </span>            : 
<span class="lineNum">     224 </span>            : 
<span class="lineNum">     225 </span><span class="lineCov">          1 : static int makemask (const char *smask, int count) {</span>
<span class="lineNum">     226 </span><span class="lineCov">          1 :   int mask = 0;</span>
<span class="lineNum">     227 </span><span class="lineCov">          1 :   if (strchr(smask, 'c')) mask |= LUA_MASKCALL;</span>
<span class="lineNum">     228 </span><span class="lineCov">          1 :   if (strchr(smask, 'r')) mask |= LUA_MASKRET;</span>
<span class="lineNum">     229 </span><span class="lineCov">          1 :   if (strchr(smask, 'l')) mask |= LUA_MASKLINE;</span>
<span class="lineNum">     230 </span><span class="lineCov">          1 :   if (count &gt; 0) mask |= LUA_MASKCOUNT;</span>
<span class="lineNum">     231 </span><span class="lineCov">          1 :   return mask;</span>
<span class="lineNum">     232 </span>            : }
<span class="lineNum">     233 </span>            : 
<span class="lineNum">     234 </span>            : 
<span class="lineNum">     235 </span><span class="lineCov">          3 : static char *unmakemask (int mask, char *smask) {</span>
<span class="lineNum">     236 </span><span class="lineCov">          3 :   int i = 0;</span>
<span class="lineNum">     237 </span><span class="lineCov">          3 :   if (mask &amp; LUA_MASKCALL) smask[i++] = 'c';</span>
<span class="lineNum">     238 </span><span class="lineCov">          3 :   if (mask &amp; LUA_MASKRET) smask[i++] = 'r';</span>
<span class="lineNum">     239 </span><span class="lineCov">          3 :   if (mask &amp; LUA_MASKLINE) smask[i++] = 'l';</span>
<span class="lineNum">     240 </span><span class="lineCov">          3 :   smask[i] = '\0';</span>
<span class="lineNum">     241 </span><span class="lineCov">          3 :   return smask;</span>
<span class="lineNum">     242 </span>            : }
<span class="lineNum">     243 </span>            : 
<span class="lineNum">     244 </span>            : 
<span class="lineNum">     245 </span><span class="lineCov">          5 : static void gethooktable (lua_State *L) {</span>
<span class="lineNum">     246 </span><span class="lineCov">          5 :   lua_pushlightuserdata(L, (void *)&amp;KEY_HOOK);</span>
<span class="lineNum">     247 </span><span class="lineCov">          5 :   lua_rawget(L, LUA_REGISTRYINDEX);</span>
<span class="lineNum">     248 </span><span class="lineCov">          5 :   if (!lua_istable(L, -1)) {</span>
<span class="lineNum">     249 </span><span class="lineCov">          1 :     lua_pop(L, 1);</span>
<span class="lineNum">     250 </span><span class="lineCov">          1 :     lua_createtable(L, 0, 1);</span>
<span class="lineNum">     251 </span><span class="lineCov">          1 :     lua_pushlightuserdata(L, (void *)&amp;KEY_HOOK);</span>
<span class="lineNum">     252 </span><span class="lineCov">          1 :     lua_pushvalue(L, -2);</span>
<span class="lineNum">     253 </span><span class="lineCov">          1 :     lua_rawset(L, LUA_REGISTRYINDEX);</span>
<span class="lineNum">     254 </span>            :   }
<span class="lineNum">     255 </span><span class="lineCov">          5 : }</span>
<span class="lineNum">     256 </span>            : 
<span class="lineNum">     257 </span>            : 
<span class="lineNum">     258 </span><span class="lineCov">          2 : static int db_sethook (lua_State *L) {</span>
<span class="lineNum">     259 </span>            :   int arg, mask, count;
<span class="lineNum">     260 </span>            :   lua_Hook func;
<span class="lineNum">     261 </span><span class="lineCov">          2 :   lua_State *L1 = getthread(L, &amp;arg);</span>
<span class="lineNum">     262 </span><span class="lineCov">          2 :   if (lua_isnoneornil(L, arg+1)) {</span>
<span class="lineNum">     263 </span><span class="lineCov">          1 :     lua_settop(L, arg+1);</span>
<span class="lineNum">     264 </span><span class="lineCov">          1 :     func = NULL; mask = 0; count = 0;  /* turn off hooks */</span>
<span class="lineNum">     265 </span>            :   }
<span class="lineNum">     266 </span>            :   else {
<span class="lineNum">     267 </span><span class="lineCov">          1 :     const char *smask = luaL_checkstring(L, arg+2);</span>
<span class="lineNum">     268 </span><span class="lineCov">          1 :     luaL_checktype(L, arg+1, LUA_TFUNCTION);</span>
<span class="lineNum">     269 </span><span class="lineCov">          1 :     count = luaL_optint(L, arg+3, 0);</span>
<span class="lineNum">     270 </span><span class="lineCov">          1 :     func = hookf; mask = makemask(smask, count);</span>
<span class="lineNum">     271 </span>            :   }
<span class="lineNum">     272 </span><span class="lineCov">          2 :   gethooktable(L);</span>
<span class="lineNum">     273 </span><span class="lineCov">          2 :   lua_pushlightuserdata(L, L1);</span>
<span class="lineNum">     274 </span><span class="lineCov">          2 :   lua_pushvalue(L, arg+1);</span>
<span class="lineNum">     275 </span><span class="lineCov">          2 :   lua_rawset(L, -3);  /* set new hook */</span>
<span class="lineNum">     276 </span><span class="lineCov">          2 :   lua_pop(L, 1);  /* remove hook table */</span>
<span class="lineNum">     277 </span><span class="lineCov">          2 :   lua_sethook(L1, func, mask, count);  /* set hooks */</span>
<span class="lineNum">     278 </span><span class="lineCov">          2 :   return 0;</span>
<span class="lineNum">     279 </span>            : }
<span class="lineNum">     280 </span>            : 
<span class="lineNum">     281 </span>            : 
<span class="lineNum">     282 </span><span class="lineCov">          3 : static int db_gethook (lua_State *L) {</span>
<span class="lineNum">     283 </span>            :   int arg;
<span class="lineNum">     284 </span><span class="lineCov">          3 :   lua_State *L1 = getthread(L, &amp;arg);</span>
<span class="lineNum">     285 </span>            :   char buff[5];
<span class="lineNum">     286 </span><span class="lineCov">          3 :   int mask = lua_gethookmask(L1);</span>
<span class="lineNum">     287 </span><span class="lineCov">          3 :   lua_Hook hook = lua_gethook(L1);</span>
<span class="lineNum">     288 </span><span class="lineCov">          3 :   if (hook != NULL &amp;&amp; hook != hookf)  /* external hook? */</span>
<span class="lineNum">     289 </span><span class="lineNoCov">          0 :     lua_pushliteral(L, &quot;external hook&quot;);</span>
<span class="lineNum">     290 </span>            :   else {
<span class="lineNum">     291 </span><span class="lineCov">          3 :     gethooktable(L);</span>
<span class="lineNum">     292 </span><span class="lineCov">          3 :     lua_pushlightuserdata(L, L1);</span>
<span class="lineNum">     293 </span><span class="lineCov">          3 :     lua_rawget(L, -2);   /* get hook */</span>
<span class="lineNum">     294 </span><span class="lineCov">          3 :     lua_remove(L, -2);  /* remove hook table */</span>
<span class="lineNum">     295 </span>            :   }
<span class="lineNum">     296 </span><span class="lineCov">          3 :   lua_pushstring(L, unmakemask(mask, buff));</span>
<span class="lineNum">     297 </span><span class="lineCov">          3 :   lua_pushinteger(L, lua_gethookcount(L1));</span>
<span class="lineNum">     298 </span><span class="lineCov">          3 :   return 3;</span>
<span class="lineNum">     299 </span>            : }
<span class="lineNum">     300 </span>            : 
<span class="lineNum">     301 </span>            : 
<span class="lineNum">     302 </span><span class="lineCov">          3 : static int db_debug (lua_State *L) {</span>
<span class="lineNum">     303 </span><span class="lineCov">          2 :   for (;;) {</span>
<span class="lineNum">     304 </span>            :     char buffer[250];
<span class="lineNum">     305 </span><span class="lineCov">          3 :     fputs(&quot;lua_debug&gt; &quot;, stderr);</span>
<span class="lineNum">     306 </span><span class="lineCov">          3 :     if (fgets(buffer, sizeof(buffer), stdin) == 0 ||</span>
<span class="lineNum">     307 </span><span class="lineCov">          3 :         strcmp(buffer, &quot;cont\n&quot;) == 0)</span>
<span class="lineNum">     308 </span><span class="lineCov">          1 :       return 0;</span>
<span class="lineNum">     309 </span><span class="lineCov">          4 :     if (luaL_loadbuffer(L, buffer, strlen(buffer), &quot;=(debug command)&quot;) ||</span>
<span class="lineNum">     310 </span><span class="lineCov">          2 :         lua_pcall(L, 0, 0, 0)) {</span>
<span class="lineNum">     311 </span><span class="lineCov">          1 :       fputs(lua_tostring(L, -1), stderr);</span>
<span class="lineNum">     312 </span><span class="lineCov">          1 :       fputs(&quot;\n&quot;, stderr);</span>
<span class="lineNum">     313 </span>            :     }
<span class="lineNum">     314 </span><span class="lineCov">          2 :     lua_settop(L, 0);  /* remove eventual returns */</span>
<span class="lineNum">     315 </span>            :   }
<span class="lineNum">     316 </span>            : }
<span class="lineNum">     317 </span>            : 
<span class="lineNum">     318 </span>            : 
<span class="lineNum">     319 </span>            : #define LEVELS1 12      /* size of the first part of the stack */
<span class="lineNum">     320 </span>            : #define LEVELS2 10      /* size of the second part of the stack */
<span class="lineNum">     321 </span>            : 
<span class="lineNum">     322 </span><span class="lineCov">          7 : static int db_errorfb (lua_State *L) {</span>
<span class="lineNum">     323 </span>            :   int level;
<span class="lineNum">     324 </span><span class="lineCov">          7 :   int firstpart = 1;  /* still before eventual `...' */</span>
<span class="lineNum">     325 </span>            :   int arg;
<span class="lineNum">     326 </span><span class="lineCov">          7 :   lua_State *L1 = getthread(L, &amp;arg);</span>
<span class="lineNum">     327 </span>            :   lua_Debug ar;
<span class="lineNum">     328 </span><span class="lineCov">          7 :   if (lua_isnumber(L, arg+2)) {</span>
<span class="lineNum">     329 </span><span class="lineCov">          4 :     level = (int)lua_tointeger(L, arg+2);</span>
<span class="lineNum">     330 </span><span class="lineCov">          4 :     lua_pop(L, 1);</span>
<span class="lineNum">     331 </span>            :   }
<span class="lineNum">     332 </span>            :   else
<span class="lineNum">     333 </span><span class="lineCov">          3 :     level = (L == L1) ? 1 : 0;  /* level 0 may be this own function */</span>
<span class="lineNum">     334 </span><span class="lineCov">          7 :   if (lua_gettop(L) == arg)</span>
<span class="lineNum">     335 </span><span class="lineCov">          1 :     lua_pushliteral(L, &quot;&quot;);</span>
<span class="lineNum">     336 </span><span class="lineCov">          6 :   else if (!lua_isstring(L, arg+1)) return 1;  /* message is not a string */</span>
<span class="lineNum">     337 </span><span class="lineCov">          5 :   else lua_pushliteral(L, &quot;\n&quot;);</span>
<span class="lineNum">     338 </span><span class="lineCov">          6 :   lua_pushliteral(L, &quot;stack traceback:&quot;);</span>
<span class="lineNum">     339 </span><span class="lineCov">         20 :   while (lua_getstack(L1, level++, &amp;ar)) {</span>
<span class="lineNum">     340 </span><span class="lineCov">         14 :     if (level &gt; LEVELS1 &amp;&amp; firstpart) {</span>
<span class="lineNum">     341 </span>            :       /* no more than `LEVELS2' more levels? */
<span class="lineNum">     342 </span><span class="lineNoCov">          0 :       if (!lua_getstack(L1, level+LEVELS2, &amp;ar))</span>
<span class="lineNum">     343 </span><span class="lineNoCov">          0 :         level--;  /* keep going */</span>
<span class="lineNum">     344 </span>            :       else {
<span class="lineNum">     345 </span><span class="lineNoCov">          0 :         lua_pushliteral(L, &quot;\n\t...&quot;);  /* too many levels */</span>
<span class="lineNum">     346 </span><span class="lineNoCov">          0 :         while (lua_getstack(L1, level+LEVELS2, &amp;ar))  /* find last levels */</span>
<span class="lineNum">     347 </span><span class="lineNoCov">          0 :           level++;</span>
<span class="lineNum">     348 </span>            :       }
<span class="lineNum">     349 </span><span class="lineNoCov">          0 :       firstpart = 0;</span>
<span class="lineNum">     350 </span><span class="lineNoCov">          0 :       continue;</span>
<span class="lineNum">     351 </span>            :     }
<span class="lineNum">     352 </span><span class="lineCov">         14 :     lua_pushliteral(L, &quot;\n\t&quot;);</span>
<span class="lineNum">     353 </span><span class="lineCov">         14 :     lua_getinfo(L1, &quot;Snl&quot;, &amp;ar);</span>
<span class="lineNum">     354 </span><span class="lineCov">         14 :     lua_pushfstring(L, &quot;%s:&quot;, ar.short_src);</span>
<span class="lineNum">     355 </span><span class="lineCov">         14 :     if (ar.currentline &gt; 0)</span>
<span class="lineNum">     356 </span><span class="lineCov">          4 :       lua_pushfstring(L, &quot;%d:&quot;, ar.currentline);</span>
<span class="lineNum">     357 </span><span class="lineCov">         14 :     if (*ar.namewhat != '\0')  /* is there a name? */</span>
<span class="lineNum">     358 </span><span class="lineCov">          2 :         lua_pushfstring(L, &quot; in function &quot; LUA_QS, ar.name);</span>
<span class="lineNum">     359 </span>            :     else {
<span class="lineNum">     360 </span><span class="lineCov">         12 :       if (*ar.what == 'm')  /* main? */</span>
<span class="lineNum">     361 </span><span class="lineCov">          4 :         lua_pushfstring(L, &quot; in main chunk&quot;);</span>
<span class="lineNum">     362 </span><span class="lineCov">          8 :       else if (*ar.what == 'C' || *ar.what == 't')</span>
<span class="lineNum">     363 </span><span class="lineCov">          8 :         lua_pushliteral(L, &quot; ?&quot;);  /* C function or tail call */</span>
<span class="lineNum">     364 </span>            :       else
<span class="lineNum">     365 </span><span class="lineNoCov">          0 :         lua_pushfstring(L, &quot; in function &lt;%s:%d&gt;&quot;,</span>
<span class="lineNum">     366 </span>            :                            ar.short_src, ar.linedefined);
<span class="lineNum">     367 </span>            :     }
<span class="lineNum">     368 </span><span class="lineCov">         14 :     lua_concat(L, lua_gettop(L) - arg);</span>
<span class="lineNum">     369 </span>            :   }
<span class="lineNum">     370 </span><span class="lineCov">          6 :   lua_concat(L, lua_gettop(L) - arg);</span>
<span class="lineNum">     371 </span><span class="lineCov">          6 :   return 1;</span>
<span class="lineNum">     372 </span>            : }
<span class="lineNum">     373 </span>            : 
<span class="lineNum">     374 </span>            : 
<span class="lineNum">     375 </span>            : static const luaL_Reg dblib[] = {
<span class="lineNum">     376 </span>            :   {&quot;debug&quot;, db_debug},
<span class="lineNum">     377 </span>            :   {&quot;getfenv&quot;, db_getfenv},
<span class="lineNum">     378 </span>            :   {&quot;gethook&quot;, db_gethook},
<span class="lineNum">     379 </span>            :   {&quot;getinfo&quot;, db_getinfo},
<span class="lineNum">     380 </span>            :   {&quot;getlocal&quot;, db_getlocal},
<span class="lineNum">     381 </span>            :   {&quot;getregistry&quot;, db_getregistry},
<span class="lineNum">     382 </span>            :   {&quot;getmetatable&quot;, db_getmetatable},
<span class="lineNum">     383 </span>            :   {&quot;getupvalue&quot;, db_getupvalue},
<span class="lineNum">     384 </span>            :   {&quot;setfenv&quot;, db_setfenv},
<span class="lineNum">     385 </span>            :   {&quot;sethook&quot;, db_sethook},
<span class="lineNum">     386 </span>            :   {&quot;setlocal&quot;, db_setlocal},
<span class="lineNum">     387 </span>            :   {&quot;setmetatable&quot;, db_setmetatable},
<span class="lineNum">     388 </span>            :   {&quot;setupvalue&quot;, db_setupvalue},
<span class="lineNum">     389 </span>            :   {&quot;traceback&quot;, db_errorfb},
<span class="lineNum">     390 </span>            :   {NULL, NULL}
<span class="lineNum">     391 </span>            : };
<span class="lineNum">     392 </span>            : 
<span class="lineNum">     393 </span>            : 
<span class="lineNum">     394 </span><span class="lineCov">         87 : LUALIB_API int luaopen_debug (lua_State *L) {</span>
<span class="lineNum">     395 </span><span class="lineCov">         87 :   luaL_register(L, LUA_DBLIBNAME, dblib);</span>
<span class="lineNum">     396 </span><span class="lineCov">         87 :   return 1;</span>
<span class="lineNum">     397 </span>            : }
<span class="lineNum">     398 </span>            : 
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.13</a></td></tr>
  </table>
  <br>

</body>
</html>
